class Rent < ActiveRecord::Base
  
validates :customer_id, :product_id, :contact_num, :presence => true
attr_accessible :contact_name, :contact_num, :customer_id, :date, :day_charge, :deposit, :expected_return, :num_days, :product_id, :returned, :invoice
validate :end_date_greater_than_start_date

	def end_date_greater_than_start_date
		errors.add(:date, "of expected return must be after start date!") if expected_return <= date
	end

	def rented_days(return_date)
		if return_date > expected_return.to_date then
			return (expected_return.to_date - date.to_date).to_i
		else
			return (return_date - date.to_date).to_i
		end
	end

	def overdue_days(return_date)
		if return_date > expected_return.to_date then
			return (return_date - expected_return.to_date).to_i
		else
			return 0
		end
	end

	def calculate_charge(return_date)
		#minimum 1 day charge
		if (return_date <= date.to_date) then
			return (day_charge * 1 - deposit)
		end
		return (day_charge * (rented_days(Time.current.to_date) 
										+ overdue_days(Time.current.to_date))- deposit) 
	end
end
